USE [msdb]
GO

/****** Object:  Job [AutomatLog_to_AutomatLogHistory_copyNight and clear LOG]    Script Date: 05/16/2012 17:13:58 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]]    Script Date: 05/16/2012 17:14:01 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'AutomatLog_to_AutomatLogHistory_copyNight and clear LOG', 
		@enabled=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=0, 
		@notify_level_netsend=0, 
		@notify_level_page=0, 
		@delete_level=0, 
		@description=N'Копирует ночью  из таблицы AutomatLog в таблицу AutomatLogHistory  (потом будет удалять это из AutomatLog)', 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'ALTER-I\knovikov', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [send mail about cashcode sum in 0:00]    Script Date: 05/16/2012 17:14:04 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'send mail about cashcode sum in 0:00', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=3, 
		@on_success_step_id=0, 
		@on_fail_action=3, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'EXEC msdb.dbo.sp_send_dbmail
	@profile_name = ''i-Dealer'', @query_result_header = 1,	@query_result_separator = '';'',	@attach_query_result_as_file = 1,
    @append_query_error = 1,--  @query_no_truncate = 1,
	@subject = ''list of CasCode sum'',	@recipients = ''nm@moneyps.ru;finance@moneyps.ru;isemenov@moneyps.ru'',
	@query  = ''exec rep_smr.[1c].[Automats_Sum_in_CashCode_forNightEmail] ''
', 
		@database_name=N'master', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [CopyAutomatLog to history]    Script Date: 05/16/2012 17:14:06 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'CopyAutomatLog to history', 
		@step_id=2, 
		@cmdexec_success_code=0, 
		@on_success_action=3, 
		@on_success_step_id=0, 
		@on_fail_action=3, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'exec i_dealer.dbo.tmp_CopyAutomatLog', 
		@database_name=N'i_dealer', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [to smr]    Script Date: 05/16/2012 17:14:06 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'to smr', 
		@step_id=3, 
		@cmdexec_success_code=0, 
		@on_success_action=3, 
		@on_success_step_id=0, 
		@on_fail_action=3, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'declare  @ErrorCode int,  @ErrorMessage varchar(max)
exec proc_smr.smr.Payment_fromIdealerCopy @ErrorCode output,  @ErrorMessage output
', 
		@database_name=N'master', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [clearLog]    Script Date: 05/16/2012 17:14:08 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'clearLog', 
		@step_id=4, 
		@cmdexec_success_code=0, 
		@on_success_action=3, 
		@on_success_step_id=0, 
		@on_fail_action=3, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'exec proc_smr.id.Delete_logRecords_Old60days', 
		@database_name=N'master', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [Send_Account_Sum]    Script Date: 05/16/2012 17:14:09 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Send_Account_Sum', 
		@step_id=5, 
		@cmdexec_success_code=0, 
		@on_success_action=1, 
		@on_success_step_id=0, 
		@on_fail_action=3, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'IF DAY(GetDate()) = 1

  EXEC msdb.dbo.sp_send_dbmail
	@profile_name = ''i-Dealer'', @query_result_header = 1,	@query_result_separator = '''',	                @append_query_error = 1,
	 @subject = ''Account Balance money-money'',	
                @recipients = ''knovikov@moneyps.ru;nm@moneyps.ru;isemenov@moneyps.ru'',      
	@query  = ''SET NOCOUNT ON;  select GetDate() as [Date]; 
                                   print "---------------------------------";
                                   exec rep_smr.[1c].[Account_Balance]''
', 
		@database_name=N'master', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [onError]    Script Date: 05/16/2012 17:14:09 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'onError', 
		@step_id=6, 
		@cmdexec_success_code=0, 
		@on_success_action=1, 
		@on_success_step_id=0, 
		@on_fail_action=2, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'declare max_id;
select max_id = max(id) from i_dealer.dbo.Log_AL_to_ALH_export ;

insert into i_dealer.dbo.Log_AL_to_ALH_export 
            (id,           
             DaTime, 
            Message, 
            ErrCode,  
            MaxAutomatLogHistory_ID, 
            Count_AL_Ins, 
            Count_ALH_Ins
            Count_Del)
values (max_id  ,   
               GETDATE(),  
               ''ошибка непонятная возникла'',    
               0,                     
              -1,
               0,                      
               0,
                0);', 
		@database_name=N'master', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Night Work', 
		@enabled=1, 
		@freq_type=4, 
		@freq_interval=1, 
		@freq_subday_type=1, 
		@freq_subday_interval=0, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=0, 
		@active_start_date=20080815, 
		@active_end_date=99991231, 
		@active_start_time=33000, 
		@active_end_time=235959, 
		@schedule_uid=N'a3d51908-7771-4ef8-af41-d5fd2e412061'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:

GO

